package com.fire.market.interceptor;

import com.fire.market.utils.TraceUtil;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * ApplicationFullInterceptor
 *
 * @author zwd
 * @date 2021/7/20
 */
public class ApplicationFullInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest servletRequest, HttpServletResponse servletResponse, Object handler) throws Exception {

        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        String uri = request.getRequestURI();
        // 初始化 TraceId
        TraceUtil.initTrace(uri);
        // 清除 TraceId 和 TraceUri
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        // 清除 TraceId 和 TraceUri
        TraceUtil.clearTrace();
    }
}
